/*
 #
 #  Files       : Expressions.h
 #                ( C++ header file )
 #
 #  Description : The SmallMatrix Library
 #                ( http://code.google.com/p/smallmatrix )
 #
 #  Copyright   : Olivier Juan
 #                ( http://www.mas.ecp.fr/vision/Personnel/juan/ )
 #
 #  License     : CeCILL-C
 #                ( http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html )
 #
 #  This software is governed by the CeCILL-C license under French law and
 #  abiding by the rules of distribution of free software.  You can  use,
 #  modify and or redistribute the software under the terms of the CeCILL-C
 #  license as circulated by CEA, CNRS and INRIA at the following URL
 #  "http://www.cecill.info".
 #
 #  As a counterpart to the access to the source code and  rights to copy,
 #  modify and redistribute granted by the license, users are provided only
 #  with a limited warranty  and the software's author,  the holder of the
 #  economic rights,  and the successive licensors  have only  limited
 #  liability.
 #
 #  In this respect, the user's attention is drawn to the risks associated
 #  with loading,  using,  modifying and/or developing or reproducing the
 #  software by the user in light of its specific status of free software,
 #  that may mean  that it is complicated to manipulate,  and  that  also
 #  therefore means  that it is reserved for developers  and  experienced
 #  professionals having in-depth computer knowledge. Users are therefore
 #  encouraged to load and test the software's suitability as regards their
 #  requirements in conditions enabling the security of their systems and/or
 #  data to be ensured and,  more generally, to use and operate it in the
 #  same conditions as regards security.
 #
 #  The fact that you are presently reading this means that you have had
 #  knowledge of the CeCILL-C license and that you accept its terms.
 #
*/

#ifndef _EXPRESSIONS_H
#define _EXPRESSIONS_H

#include <SmallMatrix/SmallMatrix.h>

NAMESPACE_BEGIN(SmallMatrix)

#pragma warning (disable : 4512)
template <bool, typename T1, typename T2>
struct Selector {
	typedef T1 result_type;
};
template <typename T1, typename T2>
struct Selector<false,T1,T2> {
	typedef T2 result_type;
};

//Binary Expressions
template <typename Functor, typename T1, typename T2=void>
struct Expressions {
	typedef typename Results<Functor::TYPE & (FUNCTOR_ASSIGN-1), T1, T2> operation1_type;
	typedef typename operation1_type::type result1_type;
	enum { OK1 = typename operation1_type::OK };
	typedef typename Results<FUNCTOR_ASSIGN, T1, result1_type> operation2_type;
	enum { OK2 = typename operation2_type::OK };
	typedef typename Selector<(Functor::TYPE & FUNCTOR_ASSIGN) == FUNCTOR_ASSIGN, typename operation2_type::type, typename operation1_type::type>::result_type result_type;
	enum { OK = OK1 && (Functor::TYPE & FUNCTOR_ASSIGN ? OK2 : true) }; 
	typedef typename Selector<(Functor::TYPE & FUNCTOR_ASSIGN) == FUNCTOR_ASSIGN, T1, const T1>::result_type CT1;
	typedef typename Selector<(Functor::TYPE & FUNCTOR_ASSIGN) == FUNCTOR_ASSIGN, reel&, reel>::result_type return_type;
	typedef const T2 CT2;
	CT1& mFirst;
	CT2& mSecond;
	//enum { OK = typename Results<Functor::TYPE,T1,T2>::OK };
	INLINE Expressions(CT1& first, CT2& second) : mFirst(first), mSecond(second) { }
	INLINE return_type operator[](const int& i) const {
		return Functor::result(mFirst[i], mSecond[i]);
	}
	INLINE return_type operator[](const int& i) {
		return Functor::result(mFirst[i], mSecond[i]);
	}
	INLINE return_type operator()(const int& i, const int& j) const {
		return Functor::result(mFirst(i,j), mSecond(i,j));
	}
	INLINE return_type operator()(const int& i, const int& j) {
		return Functor::result(mFirst(i,j), mSecond(i,j));
	}
	template <int Idx>
	INLINE return_type TabAccess() const {
		return Functor::result(mFirst.template TabAccess<Idx>(), mSecond.template TabAccess<Idx>());
	}
	template <int Idx>
	INLINE return_type TabAccess() {
		return Functor::result(mFirst.template TabAccess<Idx>(), mSecond.template TabAccess<Idx>());
	}
	template <int IdxR, int IdxC>
	INLINE return_type Access() const {
		return Functor::result(mFirst.template Access<IdxR,IdxC>(), mSecond.template Access<IdxR,IdxC>());
	}
	template <int IdxR, int IdxC>
	INLINE return_type Access() {
		return Functor::result(mFirst.template Access<IdxR,IdxC>(), mSecond.template Access<IdxR,IdxC>());
	}
};
//Binary Expressions - scalar
template <typename Functor, typename T1>
struct Expressions<Functor, T1, reel> {
	typedef typename Results<Functor::TYPE & (FUNCTOR_ASSIGN-1), T1, reel> operation1_type;
	typedef typename operation1_type::type result1_type;
	enum { OK1 = typename operation1_type::OK };
	typedef typename Results<FUNCTOR_ASSIGN, T1, result1_type> operation2_type;
	enum { OK2 = typename operation2_type::OK };
	typedef typename Selector<(Functor::TYPE & FUNCTOR_ASSIGN) == FUNCTOR_ASSIGN, typename operation2_type::type, typename operation1_type::type>::result_type result_type;
	enum { OK = OK1 && (Functor::TYPE & FUNCTOR_ASSIGN ? OK2 : true) }; 

	typedef typename Selector<(Functor::TYPE & FUNCTOR_ASSIGN) == FUNCTOR_ASSIGN, T1, const T1>::result_type CT1;
	typedef typename Selector<(Functor::TYPE & FUNCTOR_ASSIGN) == FUNCTOR_ASSIGN, reel&, reel>::result_type return_type;
	typedef const reel CT2;
	CT1& mFirst;
	CT2& mSecond;
	INLINE Expressions(CT1& first, CT2& second) : mFirst(first), mSecond(second) {}
	INLINE return_type operator[](const int& i) const {
		return Functor::result(mFirst[i], mSecond);
	}
	INLINE return_type operator[](const int& i) {
		return Functor::result(mFirst[i], mSecond);
	}
	INLINE return_type operator()(const int& i, const int& j) const {
		return Functor::result(mFirst(i,j), mSecond);
	}
	INLINE return_type operator()(const int& i, const int& j) {
		return Functor::result(mFirst(i,j), mSecond);
	}
	template <int Idx>
	INLINE return_type TabAccess() const {
		return Functor::result(mFirst.template TabAccess<Idx>(), mSecond);
	}
	template <int Idx>
	INLINE return_type TabAccess() {
		return Functor::result(mFirst.template TabAccess<Idx>(), mSecond);
	}
	template <int IdxR, int IdxC>
	INLINE return_type Access() const {
		return Functor::result(mFirst.template Access<IdxR,IdxC>(), mSecond);
	}
	template <int IdxR, int IdxC>
	INLINE return_type Access() {
		return Functor::result(mFirst.template Access<IdxR,IdxC>(), mSecond);
	}
};
template <typename Functor, typename T2>
struct Expressions<Functor, reel, T2> {
	typedef typename Results<Functor::TYPE, reel, T2> operation1_type;
	typedef typename operation1_type::type result_type;
	enum { OK = typename operation1_type::OK };

	typedef const reel CT1;
	typedef reel return_type;
	typedef const T2 CT2;
	CT1& mFirst;
	CT2& mSecond;
	INLINE Expressions(CT1& first, CT2& second) : mFirst(first), mSecond(second) {}
	INLINE return_type operator[](const int& i) const {
		return Functor::result(mFirst, mSecond[i]);
	}
	INLINE return_type operator[](const int& i) {
		return Functor::result(mFirst, mSecond[i]);
	}
	INLINE return_type operator()(const int& i, const int& j) const {
		return Functor::result(mFirst, mSecond(i,j));
	}
	INLINE return_type operator()(const int& i, const int& j) {
		return Functor::result(mFirst, mSecond(i,j));
	}
	template <int Idx>
	INLINE return_type TabAccess() const {
		return Functor::result(mFirst, mSecond.template TabAccess<Idx>());
	}
	template <int Idx>
	INLINE return_type TabAccess() {
		return Functor::result(mFirst, mSecond.template TabAccess<Idx>());
	}
	template <int IdxR, int IdxC>
	INLINE return_type Access() const {
		return Functor::result(mFirst, mSecond.template Access<IdxR,IdxC>());
	}
	template <int IdxR, int IdxC>
	INLINE return_type Access() {
		return Functor::result(mFirst, mSecond.template Access<IdxR,IdxC>());
	}
};
//Unary Expressions
template <typename Functor, typename T1>
struct Expressions<Functor, T1, void> {
	typedef typename Results<Functor::TYPE, T1, void> operation1_type;
	typedef typename operation1_type::type result_type;
	enum { OK = typename operation1_type::OK };

	typedef const T1 CT1;
	typedef reel return_type;
	CT1& mFirst;
	INLINE Expressions(CT1& first) : mFirst(first) {}
	INLINE return_type operator[](const int& i) const {
		return Functor::result(mFirst[i]);
	}
	INLINE return_type operator[](const int& i) {
		return Functor::result(mFirst[i]);
	}
	INLINE return_type operator()(const int& i, const int& j) const {
		return Functor::result(mFirst(i,j));
	}
	INLINE return_type operator()(const int& i, const int& j) {
		return Functor::result(mFirst(i,j));
	}
	template <int Idx>
	INLINE return_type TabAccess() const {
		return Functor::result(mFirst.template TabAccess<Idx>());
	}
	template <int Idx>
	INLINE return_type TabAccess() {
		return Functor::result(mFirst.template TabAccess<Idx>());
	}
	template <int IdxR, int IdxC>
	INLINE return_type Access() const {
		return Functor::result(mFirst.template Access<IdxR,IdxC>());
	}
	template <int IdxR, int IdxC>
	INLINE return_type Access() {
		return Functor::result(mFirst.template Access<IdxR,IdxC>());
	}
};


#pragma warning (default : 4512)
NAMESPACE_END

#endif
